package com.yunxi.service.localService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.sto.StoTransportPlanMapper;
import com.yunxi.model.sto.StoCarriageRoute;
import com.yunxi.model.sto.StoCarrier;
import com.yunxi.model.sto.StoTransportPlan;
import com.yunxi.service.localService.sto.StoCarriageRouteService;
import com.yunxi.service.localService.sto.StoCarrierService;
import com.yunxi.service.localService.sto.StoTransportPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 运输计划单 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-23
 */
@Service
public class StoTransportPlanServiceImpl extends ServiceImplPlus<StoTransportPlanMapper, StoTransportPlan> implements StoTransportPlanService {

    @Autowired
    @Qualifier(value = "stoCarrierServiceImpl")
    StoCarrierService stoCarrierService;

    @Autowired
    @Qualifier(value = "stoCarriageRouteServiceImpl")
    StoCarriageRouteService stoCarriageRouteService;
    
    /**
     * 取消运输计划单
     * @param list
     * @throws Exception
     */
    @Override
    public void cancel(List<String> list) throws Exception {
        if (list != null && list.size() > 0) {
            for (String id : list) {
                StoTransportPlan stoTransportPlan = baseMapper.selectById(id);
                if (stoTransportPlan != null) {
                    if (stoTransportPlan.getBillState() != null && StringUtil.isNotEmpty(stoTransportPlan.getBillState())) {
                        String billState = stoTransportPlan.getBillState();
                        if (!billState.equals("1") && !billState.equals("2") && !billState.equals("3")) {
                            throw new Exception("操作失败,已安排运输计划不可以取消!");
                        }
                    }
                    stoTransportPlan.setBillState("3");
                    baseMapper.updateById(stoTransportPlan);
                }
            }
        }

    }

    /**
     * 运输计划维护运输路线
     * @param transportPlanId
     * @param routeCode
     * @throws Exception
     */
    @Override
    public void route(String transportPlanId, String routeCode) throws Exception {
        StoTransportPlan stoTransportPlan = baseMapper.selectById(transportPlanId);
        QueryWrapper<StoCarriageRoute> queryWrapper = new QueryWrapper();
        queryWrapper.eq("ROUTE_CODE",routeCode);
        StoCarriageRoute stoCarriageRoute = stoCarriageRouteService.getOne(queryWrapper);
        if(stoTransportPlan != null){
            stoTransportPlan.setRouteCode(routeCode);
            if(stoCarriageRoute != null){
                stoTransportPlan.setStartCityCode(stoCarriageRoute.getStartCityCode());
                stoTransportPlan.setStartCityName(stoCarriageRoute.getStartCityName());
                stoTransportPlan.setEndCityCode(stoCarriageRoute.getEndCityCode());
                stoTransportPlan.setEndCityName(stoCarriageRoute.getEndCityName());
            }
            baseMapper.updateById(stoTransportPlan);
        }
        else{
            throw new Exception("操作失败,无法根据id"+transportPlanId+"找到对应运输计划单!");
        }
    }

    /**
     * 根据运输路线代码获取相关承运商列表
     * @param routeCode
     * @return
     */
    @Override
    public List carrierList(String routeCode) {
        return baseMapper.carrierList(routeCode);
    }

    /**
     * 运输计划维护承运商
     * @param transportPlanId
     * @param carrierCode
     */
    @Override
    public void carrier(String transportPlanId, String carrierCode) throws Exception{
        StoTransportPlan stoTransportPlan = baseMapper.selectById(transportPlanId);
        QueryWrapper<StoCarrier> queryWrapper = new QueryWrapper();
        queryWrapper.eq("CARRIER_CODE",carrierCode);
        StoCarrier stoCarrier = stoCarrierService.getOne(queryWrapper);
        if(stoTransportPlan != null){
            stoTransportPlan.setCarrierCode(carrierCode);
            if(stoCarrier != null){
                stoTransportPlan.setCarrierName(stoCarrier.getCarrierName());//承运商名称
            }
            baseMapper.updateById(stoTransportPlan);
        }
        else{
            throw new Exception("操作失败,无法根据id"+transportPlanId+"找到对应运输计划单!");
        }
    }

    @Override
    public Boolean checkAndChangeTransportPlan(String vin, String billState) throws Exception {
        Boolean result = false;
        //查询VIN对应的运输计划行数
        QueryWrapper<StoTransportPlan> query = new QueryWrapper<>();
        query.eq("VIN",vin);
        int count = baseMapper.selectCount(query);

        //查询VIN对应指定运输计划状态
        QueryWrapper<StoTransportPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("VIN",vin);
        queryWrapper.eq("BILL_STATE",1);
        queryWrapper.orderByDesc("CREATE_TIME");
        queryWrapper.last("limit 1");
        StoTransportPlan stoTransportPlan = baseMapper.selectOne(queryWrapper);

        /**
         *  参数billState = 1 时检查 VIN对应运输计划是否下发到TMS
         *  以下情况为未下发
         *  1.VIN在运输计划表查不到对应的行
         *  2.运输计划表中有状态为1的行
         *  返回 true
         *
         *  参数 billState = 3时
         *  取消 对应VIN 运输计划
         *
         */

        if(billState != null && billState.equals("1")&&count!=0){
            if(stoTransportPlan != null){
                result = true;
            }
        }else if(billState != null && billState.equals("1")&&count==0){

            result = true;
        }
        else if(billState != null && billState.equals("3")){
            if(stoTransportPlan != null){
                stoTransportPlan.setBillState(billState);
                baseMapper.updateById(stoTransportPlan);//取消运输计划
                result = true;
            }
        }
        return result;
    }
}
